MotionSenseDIR = fullfile("..", "motion-sense-master", "data", "A_DeviceMotion_data");

label_names = dictionary(["dws", "jog", "sit", "std", "ups", "wlk"], [3, 6, 4, 5, 2, 1]);

train_X = [];
train_y = [];

for label_name = ["dws", "sit", "std", "ups", "wlk"]
    for i = 1:16
        if (exist(fullfile(MotionSenseDIR, strcat(label_name, "_", num2str(i))), "dir"))
            root_path = fullfile(MotionSenseDIR, strcat(label_name, "_", num2str(i)));
            files = dir(fullfile(root_path, "sub_*.csv"));
            for file = 1:length(files)
                data = readtable(fullfile(root_path, files(file).name));
                
                data = data(:, ["userAcceleration_x", "userAcceleration_y", "userAcceleration_z", ...
                    "rotationRate_x", "rotationRate_y", "rotationRate_z"]);
                
                data = table2array(data);
                
                k = floor(size(data, 1) / 128);
                data = reshape(data(1:k*128, :, :), [128, k, 6]);
                data = permute(data, [2, 1, 3]);
                label = repmat(label_names(label_name), [k, 1]);

                train_X = cat(1, train_X, single(data));
                train_y = cat(1, train_y, uint8(label));
            end
        end
    end
end

save("MotionSense_Train.mat", "train_X", "train_y");
